# -*- mode: python ; coding: utf-8 -*-
# 执行命令：pyinstaller build.spec
# 如果打包时，文件过大，打包上来了无关依赖包，用以下步骤解决：
# 1. 删除旧的 venv 环境（PowerShell 方式）
#Remove-Item -Recurse -Force venv -ErrorAction SilentlyContinue
# 2. 创建新的 venv 环境
#python -m venv venv
# 3. 激活环境
#.\venv\Scripts\activate
# 4. 安装项目依赖
#pip install -r requirements.txt

import os
import sys
sys.setrecursionlimit(sys.getrecursionlimit() * 5)

# 设置输出路径
output_dir = r"C:\Users\user\Desktop"
block_cipher = None

a = Analysis(
    ['cni_analog_iot_device.py'],
    pathex=[],
    binaries=[],
    datas=[
        ('app_data.json', '.'),
        ('get_device_triples_info.py', '.'),
        ('aliyun_mqtt_client.py', '.'),
        ('MqttSign.py', '.'),
        # 添加 Crypto 库的数据文件
        (r'E:\alphacloud_gitee\iot_tool\venv\Lib\site-packages\Crypto','Crypto')
    ],
    hiddenimports=[
        'aliyun_mqtt_client',
        'get_device_triples_info',
        'hmac',
        'hashlib'
        'MqttSign',
        'paho.mqtt.client',
        'requests',
        'Crypto',
        'Crypto.Cipher',
        'Crypto.Util.Padding',
        'Crypto.Cipher._mode_ecb'
    ],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[
        # 常见“体积大户”，根据你项目情况排除
        'tkinter',
        'unittest',
        'pytest',
        'distutils',
        'setuptools',
        'html',
        'xml',
        'pydoc',
        'pdb',
        'numpy.f2py',
        'numpy.random',
        'pandas.io',
        'pandas.plotting',
        'matplotlib',
        'PIL',
        'scipy',
        'sklearn',
        'sqlalchemy',
        'zmq',
        'IPython',
        'jinja2',
        'pygments',
    ],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    # 打包后保存的路径
    name=os.path.join(output_dir, 'cni_analog_iot_device.exe'),
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=False,
    # exe文件的图标
    icon="logo.ico",
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='cni_analog_iot_device',
)